using System;
using System.Globalization;
using PickGold.Charting;

namespace PickGold.Charting.ChartTypes
{
	internal class HundredPercentStackedAreaChart : StackedAreaChart
	{
		private double[] _totalPerPoint;

		public HundredPercentStackedAreaChart()
		{
			base.hundredPercentStacked = true;
		}

		public override double GetYValue(CommonElements common, ChartArea area, Series series, DataPoint point, int pointIndex, int yValueIndex)
		{
			if (this._totalPerPoint == null)
			{
				int num = 0;
				foreach (Series series2 in common.DataManager.Series)
				{
					if (((string.Compare(series2.ChartTypeName, this.Name, true, CultureInfo.CurrentCulture) == 0) && (series2.ChartArea == area.Name)) && series2.IsVisible())
					{
						num++;
					}
				}
				Series[] seriesArray = new Series[num];
				int num2 = 0;
				foreach (Series series3 in common.DataManager.Series)
				{
					if (((string.Compare(series3.ChartTypeName, this.Name, true, CultureInfo.CurrentCulture) == 0) && (series3.ChartArea == area.Name)) && series3.IsVisible())
					{
						seriesArray[num2++] = series3;
					}
				}
				common.DataManipulator.CheckXValuesAlignment(seriesArray);
				this._totalPerPoint = new double[series.Points.Count];
				for (int i = 0; i < series.Points.Count; i++)
				{
					this._totalPerPoint[i] = 0.0;
					foreach (Series series4 in seriesArray)
					{
						this._totalPerPoint[i] += Math.Abs(series4.Points[i].YValues[0]);
					}
				}
			}
			if (!area.Area3DStyle.Enable3D)
			{
				if (this._totalPerPoint[pointIndex] != 0.0)
				{
					return ((point.YValues[0] / this._totalPerPoint[pointIndex]) * 100.0);
				}
				int num4 = 0;
				foreach (Series series5 in common.DataManager.Series)
				{
					if (((string.Compare(series5.ChartTypeName, this.Name, true, CultureInfo.CurrentCulture) == 0) && (series5.ChartArea == area.Name)) && series5.IsVisible())
					{
						num4++;
					}
				}
				return (100.0 / ((double)num4));
			}
			double naN = double.NaN;
			if (yValueIndex == -1)
			{
				double crossing = area.GetAxis(AxisName.Y, series.YAxisType, series.YSubAxisName).Crossing;
				naN = this.GetYValue(common, area, series, point, pointIndex, 0);
				if (area.Area3DStyle.Enable3D && (naN < 0.0))
				{
					naN = -naN;
				}
				if (naN >= 0.0)
				{
					if (!double.IsNaN(base.prevPosY))
					{
						crossing = base.prevPosY;
					}
				}
				else if (!double.IsNaN(base.prevNegY))
				{
					crossing = base.prevNegY;
				}
				return (naN - crossing);
			}
			base.prevPosY = double.NaN;
			base.prevNegY = double.NaN;
			base.prevPositionX = double.NaN;
			foreach (Series series6 in common.DataManager.Series)
			{
				if (((string.Compare(series.ChartArea, series6.ChartArea, true, CultureInfo.CurrentCulture) != 0) || (string.Compare(series.ChartTypeName, series6.ChartTypeName, true, CultureInfo.CurrentCulture) != 0)) || !series.IsVisible())
				{
					continue;
				}
				naN = (series6.Points[pointIndex].YValues[0] / this._totalPerPoint[pointIndex]) * 100.0;
				if ((!double.IsNaN(naN) && area.Area3DStyle.Enable3D) && (naN < 0.0))
				{
					naN = -naN;
				}
				if ((naN >= 0.0) && !double.IsNaN(base.prevPosY))
				{
					naN += base.prevPosY;
				}
				if ((naN < 0.0) && !double.IsNaN(base.prevNegY))
				{
					naN += base.prevNegY;
				}
				if (string.Compare(series.Name, series6.Name, StringComparison.Ordinal) == 0)
				{
					break;
				}
				if (naN >= 0.0)
				{
					base.prevPosY = naN;
				}
				else
				{
					base.prevNegY = naN;
				}
				base.prevPositionX = series6.Points[pointIndex].XValue;
				if ((base.prevPositionX == 0.0) && ChartHelper.IndexedSeries(series))
				{
					base.prevPositionX = pointIndex + 1;
				}
			}
			if (naN > 100.0)
			{
				return 100.0;
			}
			return naN;
		}

		public override void Paint(ChartGraphics graph, CommonElements common, ChartArea area, Series seriesToDraw)
		{
			base.Common = common;
			this._totalPerPoint = null;
			base.Paint(graph, common, area, seriesToDraw);
		}

		public override bool HundredPercent
		{
			get
			{
				return true;
			}
		}

		public override string Name
		{
			get
			{
				return "100%StackedArea";
			}
		}
	}
}

